package cn.cpf.app.gobang.ctrl;

import cn.cpf.app.gobang.algorithm.AlphaBeta;
import cn.cpf.app.gobang.frame.GoBangPanel;
import cn.cpf.app.gobang.global.Global;
import cn.cpf.app.gobang.modal.PieceFactory;
import cn.cpf.app.gobang.modal.Place;
import cn.cpf.app.gobang.modal.PlayerType;
import com.github.cosycode.common.ext.hub.Throws;

import javax.swing.*;

/**
 * <b>Description : </b>
 * <p>
 * <b>created in </b> 2020/3/9
 * </p>
 *
 * @author CPF
 * @since 0.1
 **/
public class AppContext {

    private final CommandExecutor executor;

    private final GoBangPanel goBangPanel;

    public AppContext(GoBangPanel goBangPanel) {
        this.goBangPanel = goBangPanel;
        this.executor = new CommandExecutor();
    }

    /**
     * 落子函数
     */
    public boolean pushPiece(Place place) {
        goBangPanel.add(PieceFactory.getPieceByPart(Global.getSituation().getCurPart(), place));
        boolean isWin = Global.getSituation().realLocatePiece(place, Global.getSituation().getCurPart());
        goBangPanel.repaint();
        return isWin;
    }

    /**
     * 获取COM运算后得到的位置
     * @return 评估的位置
     */
    public static Place getEvaluatedPlace(){
        return new AlphaBeta().getEvaluatedPlace(Global.getSituation().getCurPart());
    }

    /**
     * 撤回一步棋
     *
     * @return true, 撤回成功; false: 撤回失败(已经撤销到初始状态, 无法继续撤销)
     */
    // TODO
    public boolean rollbackOneStep() {
        return true;
    }

    /**
     * @return
     */
    // TODO
    public Object aiRunOneTime() {

        return true;
    }

    /**
     * @param title 标题
     * @param msg 信息
     * @param msgType 消息类型
     */
    public void showMessage(String title, String msg, Integer msgType) {
        final int type = msgType == null ? JOptionPane.INFORMATION_MESSAGE : msgType;
        JOptionPane.showMessageDialog(goBangPanel, msg, title, type);
    }

    /**
     * @param title 标题
     * @param e     异常
     */
    public void showMessage(String title, Exception e) {
        showMessage(title, e.getMessage(), JOptionPane.ERROR_MESSAGE);
    }

}
